package cn.edu.ctbu.datajpa01.service;


import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public List<Student> findAll(){
        return studentRepository.findAll();

    }
    /**
     * 按id进行查询
     * @param id 主键，整数
     * @return
     * **/
    public Student findByTd(Integer id){


        return studentRepository.findById(id).orElse(null);
    }


    /**
     * 按名字进行查询 like
     * @param name 主键，整数
     * @return
     * **/
    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }


    /**
     * 按名字和密码进行查询进行查询 like
     * @param name 主键，整数
     * @parama password
     * @return
     * **/
    public List<Student> findByNameAndPassword(String name,String password){


        return studentRepository.findByNameAndPassword(name,password);
    }

    public void insert(Student student){
        studentRepository.save(student);
    }
    public void update(Student student){
        studentRepository.save(student);
    }
    
    public void delete(Integer id){
        studentRepository.deleteById(id);
    }


    public Student findById(int i) {
        return studentRepository.findById(i).orElse(null);
    }
}
